<!DOCTYPE html>

<script type="text/html" data-template-name="daemon">
    <div class="form-row">
        <label for="node-input-command"><i class="fa fa-file"></i> Command</label>
        <input type="text" id="node-input-command" placeholder="command">
    </div>
    <div class="form-row">
        <label for="node-input-args"><i class="fa fa-list"></i> Arguments</label>
        <input type="text" id="node-input-args" placeholder="space separated arguments">
    </div>
    <div class="form-row">
        <label>&nbsp;</label>
        <input type="checkbox" id="node-input-autorun" style="display:inline-block; width: auto; vertical-align:baseline;">
        <label for="node-input-autorun" style="width: 70%;">Auto-start daemon on deploy ?</label>
    </div>
    <div class="form-row">
        <label>&nbsp;</label>
        <input type="checkbox" id="node-input-cr" style="display:inline-block; width:auto; vertical-align:baseline;">
        <label for="node-input-cr" style="width: 70%;">Add [enter] to every message sent ?</label>
    </div>
        <div class="form-row">
        <label>&nbsp;</label>
        <input type="checkbox" id="node-input-redo" style="display:inline-block; width:auto; vertical-align:baseline;">
        <label for="node-input-redo" style="width: 70%;">Relaunch command on exit or error ?</label>
    </div>
    <div class="form-row">
        <label for="node-input-op"> and</label>
        <select id="node-input-op" style='width:70%'>
            <option value="buffer">leave reply as a buffer</option>
            <option value="string">format reply as a string</option>
            <option value="lines">output lines of text</option>
            <option value="number">try to convert reply to a number</option>
        </select>
    </div>
    <div class="form-row">
        <label for="node-input-closer"> on close</label>
        <select id="node-input-closer" style='width:70%'>
            <option value="SIGKILL">kill process immediately</option>
            <option value="SIGTERM">try to terminate process</option>
            <option value="SIGINT">try to interrupt process</option>
        </select>
    </div>
    <div class="form-row">
        <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
        <input type="text" id="node-input-name" placeholder="Name">
    </div>
    <div class="form-tips"><b>Note:</b> The command should just be the actual command.
    All parameters should be passed in as arguments.</div>
</script>

<script type="text/javascript">
    RED.nodes.registerType('daemon',{
        category: 'function',
        color:"darksalmon",
        defaults: {
            name: {value:""},
            command: {value:"",required:true},
            args: {value:""},
            autorun: {value:true},
            cr: {value:false},
            redo: {value:true},
            op: {value:"string"},
            closer: {value:"SIGKILL"}
        },
        inputs:1,
        outputs:3,
        inputLabels: "stdin",
        outputLabels: ["stdout","stderr","exit code"],
        icon: "arrow-in.png",
        label: function() {
            return this.name||this.command.replace(/\\n /g,"\\\\n ")||"daemon";
        },
        labelStyle: function() {
            return this.name?"node_label_italic":"";
        },
        oneditprepare: function() {
            if (this.autorun === undefined) { $("#node-input-autorun").prop('checked', true); }
        }
    });
</script>
